package com.sosee.cgfw.friends;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;

/**
 * Created by Thinkpad on 2016/1/28.
 */
public class MyApplication extends Application {
    PendingIntent restartIntent;

    @Override
    public void onCreate() {
        super.onCreate();
        RongIM.init(getApplicationContext());
        initData();
        // 以下用来捕获程序崩溃异常
        Intent intent = new Intent();
        // 参数1：包名，参数2：程序入口的activity
        intent.setClassName("com.example.sousee.sousee", "com.example.sousee.sousee.activity.MainActivity");
        restartIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                intent, Intent.FLAG_ACTIVITY_NEW_TASK);
        Thread.setDefaultUncaughtExceptionHandler(restartHandler); // 程序崩溃时触发线程
    }
    //初始化
    public void initData(){

        Utils utils=new Utils(getApplicationContext());

        RongIM.setConversationBehaviorListener(new RongIM.ConversationBehaviorListener() {


            @Override
            public boolean onUserPortraitClick(Context context, Conversation.ConversationType conversationType, UserInfo userInfo) {


                //在这里处理你想要跳转的activity，示例代码为YourAcitivy

                Intent in = new Intent(context, FriendsTogetherInfoss.class);
                in.putExtra("togethers", userInfo.getUserId());
                context.startActivity(in);
                return true;

            }

            @Override
            public boolean onUserPortraitLongClick(Context context, Conversation.ConversationType conversationType, UserInfo userInfo) {
                return false;
            }

            @Override
            public boolean onMessageClick(Context context, View view, Message message) {
                return false;
            }

            @Override
            public boolean onMessageLinkClick(Context context, String s) {
                return false;
            }

            @Override
            public boolean onMessageLongClick(Context context, View view, Message message) {
                return false;
            }
        });

    }


    public Thread.UncaughtExceptionHandler restartHandler = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            LogUtils.log_E(ex.toString());
            AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
                    restartIntent); // 1秒钟后重启应用
        }
    };



}
